﻿// --------------------------------------------------------------------------------------
// <copyright file="adminController.cs" company="NGroup">
// Copyright (c) NGroup. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------
// <history>
//     <change who="Bui Thanh Nam" date="2013.12.07">Create</change>
// </history>
// --------------------------------------------------------------------------------------

namespace NGroup.VTL.Web.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Web.Mvc;
    using NGroup.VTL.Core.Authentication;
    using NGroup.VTL.Core.Authorization;
    using NGroup.VTL.Core.Const;
    using NGroup.VTL.Models;
    using NGroup.VTL.Service;
    using NGroup.VTL.Service.Abstraction;
    using NGroup.VTL.WebCore;

    public class vuaraoController : ContentBaseController
    {
        #region Constructor

        private IAdministrativeService _administrativeService;
        private IResourceService _resourceService;
        protected readonly UserSession _currentSession;
        private IAuthentication _formAuthenticator;

        public vuaraoController()
        {
            _administrativeService = new AdministrativeService();
            _formAuthenticator = new FormAuthentication();
            _currentSession = new UserSession();
            _resourceService = new ResourceService();
        }

        public vuaraoController(IAdministrativeService administrativeService)
        {
            _administrativeService = administrativeService;
        }

        public string CurrentUsername { get { return _currentSession.Username; } }

        #endregion

        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult danh_sach()
        {
            return View();
        }

        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult danh_sach_san_pham()
        {
            PagingInfoModel pagingInfo = this.GetPagingParameters();

            string author = Request.Form["filter_author"];

            if (string.IsNullOrWhiteSpace(author))
            {
                author = null;
            }

            string productType = Request.Form["filter_productType"];

            if (string.IsNullOrWhiteSpace(productType))
            {
                productType = null;
            }

            string productSupType = Request.Form["filter_productSupType"];

            if (string.IsNullOrWhiteSpace(productSupType))
            {
                productSupType = null;
            }

            List<ProductModel> productList = _administrativeService.GetProductListByPagingInfo(author, productSupType, productType, ref pagingInfo);
            Dictionary<string, string> distinctAuthors = this.SelectDistinctAuthors(productList);

            ViewBag.DistinctAuthors = distinctAuthors;
            ViewBag.PagingInfo = pagingInfo;
            ViewBag.currentFilterAuthor = author;
            ViewBag.currentFilterProduct = productType;
            ViewBag.currentFilterSupProduct = productSupType;

            return View(productList);
        }

        [HttpGet]
        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult dang_san_pham_moi()
        {
            var model = new ProductModel();
            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult dang_san_pham_moi(ProductModel model)
        {
            if (ModelState.IsValid)
            {
                model.CreatedAt = DateTime.Now;
                model.CreatedBy = CurrentUsername;
                model.IsActive = true;

                bool isAdded = _administrativeService.AddNewProduct(model);

                if (isAdded)
                {
                    _resourceService.AddSitemapEntry(model.GetFullURL());
                    return RedirectToAction("danh_sach_san_pham");
                }

                return Redirect("/loi/thao-tac-khong-hop-le");

            }

            return View(model);
        }

        [HttpGet]
        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult dang_bai_viet_moi()
        {
            return View(new NewsModel());
        }

        [HttpPost]
        [ValidateInput(false)]
        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult dang_bai_viet_moi(NewsModel model)
        {
            if (model != null)
            {
                model.TypeID = AppContentTypes.All.Find(e => e.UniqueString == "tin-tuc").TypeID;

                ModelState.Clear();
                TryValidateModel(model);
            }

            if (ModelState.IsValid)
            {
                model.CreatedAt = DateTime.Now;
                model.CreatedBy = CurrentUsername;
                model.IsActive = true;

                bool isAdded = _administrativeService.AddNewNews(model);

                if (isAdded)
                {
                    _resourceService.AddSitemapEntry(model.GetFullURL());
                    return RedirectToAction("danh_sach_bai_viet");
                }

                return Redirect("/loi/thao-tac-khong-hop-le");
            }

            return View(model);
        }

        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult delete_news(string id)
        {
            NewsModel model = new NewsModel();
            if (!string.IsNullOrEmpty(id))
            {
                int newsID;
                int typeID;                

                string[] tmp = id.Split('~');

                if (tmp != null && tmp.Length == 3)
                {
                    int.TryParse(tmp[0], out newsID);
                    int.TryParse(tmp[1], out typeID);
                    model.NewsID = newsID;
                    model.TypeID = typeID;
                    model.UniqueString = tmp[2];
                    model.LastUpdatedAt = DateTime.Now;
                    model.LastUpdatedBy = CurrentUsername;
                    model.IsActive = false;
                    bool isDeleted = _administrativeService.DeletePNews(model);

                    if (isDeleted)
                    {
                        _resourceService.RemoveSitemapEntry(model.GetFullURL());
                        return RedirectToAction("danh_sach_bai_viet");
                    }
                }

                return RedirectToAction("khong_du_quyen");
            }

            return RedirectToAction("khong_du_quyen");
        }

        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult delete_product(string id)
        {
            ProductModel model = new ProductModel();
            if (!string.IsNullOrEmpty(id))
            {
                int ProductID;
                int SupTypeID;
                int TypeID;

                string[] tmp = id.Split('~');

                if (tmp != null && tmp.Length == 4)
                {
                    int.TryParse(tmp[0], out ProductID);
                    int.TryParse(tmp[1], out SupTypeID);
                    int.TryParse(tmp[2], out TypeID);

                    model.ProductID = ProductID;
                    model.SupTypeID = SupTypeID;
                    model.TypeID = TypeID;
                    model.UniqueString = tmp[3];
                    model.LastUpdatedAt = DateTime.Now;
                    model.LastUpdatedBy = CurrentUsername;
                    model.IsActive = false;
                    bool isDeleted = _administrativeService.DeleteProduct(model);

                    if (isDeleted)
                    {
                        _resourceService.RemoveSitemapEntry(model.GetFullURL());
                        return RedirectToAction("danh_sach_san_pham");
                    }
                    return RedirectToAction("khong_du_quyen");
                }
            }
            return RedirectToAction("khong_du_quyen");
        }

        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult delete_order(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                int orderId;

                if (int.TryParse(id, out orderId))
                {
                    bool isDeleted = _administrativeService.DeleteOrder(orderId);

                    if (isDeleted)
                    {
                        return RedirectToAction("quan_ly_don_hang");
                    }
                }

                return RedirectToAction("khong_du_quyen");
            }

            return RedirectToAction("khong_du_quyen");
        }

        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult delete_user(string id)
        {
            UserModel model = new UserModel();
            if (!string.IsNullOrEmpty(id))
            {
                int UserID;
                int.TryParse(id, out UserID);
                model.UserID = UserID;
                model.LastUpdatedAt = DateTime.Now;
                model.LastUpdatedBy = CurrentUsername;
                model.IsActive = false;
                bool isDeleted = _administrativeService.DeleteUser(model);

                if (isDeleted)
                {
                    return RedirectToAction("quan_ly_nguoi_dung");
                }
                return RedirectToAction("khong_du_quyen");
            }
            return RedirectToAction("khong_du_quyen");
        }

        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult danh_sach_bai_viet()
        {
            PagingInfoModel pagingInfo = this.GetPagingParameters();
            string author = Request.Form["filter_author"];

            if (string.IsNullOrWhiteSpace(author) || author == "-1")
            {
                author = null;
            }

            string productType = Request.Form["filter_newsType"];

            int newsTypeValue;

            if (!int.TryParse(productType, out newsTypeValue))
            {
                newsTypeValue = -1;
            }

            List<NewsModel> newsList = _administrativeService.GetNewsListByPagingInfo(author, newsTypeValue, ref pagingInfo);
            Dictionary<string, string> distinctAuthors = this.SelectDistinctAuthors(newsList);

            ViewBag.DistinctAuthors = distinctAuthors;
            ViewBag.PagingInfo = pagingInfo;
            ViewBag.currentFilterAuthor = author;
            ViewBag.currentFilterNews = newsTypeValue;

            return View(newsList);
        }

        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult quan_ly_nguoi_dung()
        {
            PagingInfoModel pagingInfo = new PagingInfoModel();

            var userList = _administrativeService.GetUserListByPagingInfo(_currentSession.RoleID, ref pagingInfo);

            ViewBag.UserList = userList;

            return View();
        }

        [HttpGet]
        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult sua_bai_viet(string id)
        {
            var model = _administrativeService.GetNewsDetail(id, CurrentLanguage);
            ViewBag.UniqueString = model.Title;
            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult sua_bai_viet(NewsModel model)
        {
            if (model != null)
            {
                if (
                    AppContentTypes.All.Find(e => e.TypeID == model.TypeID &&
                        (e.UniqueString == "lien-he"
                           || e.UniqueString == "khach-hang"
                           || e.UniqueString == "sitemap"
                           || e.UniqueString == "huong-dan-thanh-toan")) != null)
                {
                    model.ImageSmallURL = "set: no images";
                }

                ModelState.Clear();
                TryValidateModel(model);
            }

            if (ModelState.IsValid)
            {
                model.LastUpdatedAt = DateTime.Now;
                model.LastUpdatedBy = CurrentUsername;
                model.IsActive = true;

                _administrativeService.UpdateNews(model);
                _resourceService.UpdateSitemapEntry(model.GetFullURL());
                
                return RedirectToAction("danh_sach_bai_viet");
            }

            return View();
        }

        [HttpGet]
        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult sua_san_pham(string parentIDString, string childIDString, string uniqueString)
        {
            int parentID = AppContentTypes.All.Find(e => !e.SupTypeID.HasValue && e.UniqueString == parentIDString).TypeID.Value;
            int childID = AppContentTypes.All.Find(e => e.SupTypeID == parentID && e.UniqueString == childIDString).TypeID.Value;

            var model = _administrativeService.GetProductDetail(parentID, childID, uniqueString);

            ViewBag.UniqueString = model.Title;
            ViewBag.CurrentChildID = childID;
            ViewBag.CurrentParentID = parentID;
            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult sua_san_pham(ProductModel model)
        {
            if (ModelState.IsValid)
            {
                model.LastUpdatedAt = DateTime.Now;
                model.LastUpdatedBy = _currentSession.Username;
                model.IsActive = true;
                _administrativeService.UpdateProduct(model);
                _resourceService.UpdateSitemapEntry(model.GetFullURL());
                return RedirectToAction("danh_sach_san_pham");
            }
            return View();
        }

        [HttpGet]
        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult quan_ly_ngon_ngu()
        {
            List<TranslationModel> translations = _resourceService.GetAllTranslations();

            return View(translations);
        }

        [HttpPost]
        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult chinh_sua_chuyen_ngu(string transVi, string transEn, string transKey)
        {
            if (!string.IsNullOrEmpty(transKey) && !string.IsNullOrEmpty(transVi) && !string.IsNullOrEmpty(transEn))
            {
                bool updated = _resourceService.UpdateTranslationItem(new TranslationModel { Key = transKey, Vietnamese = transVi, English = transEn });
            }

            return RedirectToAction("quan_ly_ngon_ngu");
        }

        [HttpPost]
        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult them_nguoi_dung(string userAlias, string username, string password)
        {
            if (!string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password))
            {
                UserModel user = new UserModel();
                if (ModelState.IsValid)
                {
                    user.UserAlias = userAlias;
                    user.Username = username;
                    user.Password = password;
                    user.CreatedBy = _currentSession.Username;
                    user.RoleID = Convert.ToInt32(VTLPermission.Admin);

                    bool isAdded = _administrativeService.AddNewUser(user);

                    if (isAdded)
                    {
                        return RedirectToAction("quan_ly_nguoi_dung");
                    }
                    else
                    {
                        return Redirect("/loi/thao-tac-khong-hop-le");
                    }
                }
            }
            return View();
        }

        [HttpPost]
        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult sua_nguoi_dung(string editUserAlias, string editUsername, string editPassword)
        {
            if (!string.IsNullOrWhiteSpace(editPassword) && !string.IsNullOrWhiteSpace(editUsername))
            {
                UserModel user = new UserModel();
                if (ModelState.IsValid)
                {
                    user.UserAlias = editUserAlias;
                    user.Username = editUsername;
                    user.Password = editPassword;
                    user.LastUpdatedBy = _currentSession.Username;
                    user.LastUpdatedAt = DateTime.Now;

                    bool isUpdate = _administrativeService.UpdateUser(user);

                    if (isUpdate)
                    {
                        return RedirectToAction("quan_ly_nguoi_dung");
                    }
                    else
                    {
                        return Redirect("/loi/thao-tac-khong-hop-le");
                    }
                }
            }
            return View();
        }

        public ActionResult dang_nhap(string vtlUsername, string vtlPassword)
        {
            if ((_currentSession != null && !_currentSession.IsAuthenticated) || _currentSession == null)
            {
                if (string.IsNullOrWhiteSpace(vtlUsername) || string.IsNullOrWhiteSpace(vtlPassword))
                {
                    return View();
                }

                UserModel userInfoObj = _administrativeService.GetUserInformation(vtlUsername, vtlPassword);

                if (userInfoObj != null)
                {
                    this.UpdateCurrentSession(userInfoObj);
                    _formAuthenticator.Signin(vtlUsername, false);

                    return RedirectToAction("danh_sach");
                }

                return View();
            }

            return RedirectToAction("danh_sach");
        }

        public ActionResult dang_xuat()
        {
            _formAuthenticator.Signout();
            return RedirectToAction("dang_nhap");
        }

        private void UpdateCurrentSession(UserModel userModel)
        {
            _currentSession.Password = userModel.Password;
            _currentSession.RoleID = userModel.RoleID;
            _currentSession.SessionDateTime = DateTime.Now;
            _currentSession.UserID = userModel.UserID;
            _currentSession.Username = userModel.Username;
            _currentSession.UserID = userModel.UserID;
        }

        [HttpPost]
        public bool CheckUniqueString(string uniqueString, string typeID)
        {
            int typeIdValue = 0;

            if (int.TryParse(typeID, out typeIdValue))
            {
                return _administrativeService.CheckUniqueString(typeIdValue, uniqueString);
            }

            // return existing
            return true;
        }

        [HttpGet]
        public int GetPendingOrdersCount()
        {
            return _administrativeService.GetPendingOrdersCount();
        }

        private PagingInfoModel GetPagingParameters()
        {
            var pagingInfo = new PagingInfoModel();

            string currentPage = Request.Form["currentPage"];
            string pageSize = Request.Form["pageSize"];

            int currentPageValue, pageSizeValue;

            pagingInfo.CurrentPage = int.TryParse(currentPage, out currentPageValue) ? currentPageValue : 1;
            pagingInfo.PageSize = int.TryParse(pageSize, out pageSizeValue) ? pageSizeValue : GeneralConfiguration.PageSize;

            return pagingInfo;
        }

        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult quan_ly_loai_san_pham()
        {
            List<GenericTypeItem> GenericTypeItems = AppContentTypes.All.FindAll(e => !e.CanEdit.HasValue || e.CanEdit.Value);
            return View(GenericTypeItems);
        }

        [HttpPost]
        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult quan_ly_loai_san_pham(string typeName, string typeNameEn, string typeLink, string metaKeyword, string metaKeywordEn, string metaDes, string metaDesEn, string typeID, string CanEdit, string parentID)
        {
            bool result = false;
            int parentIDInt;
            int typeIDInt;
            var model = new GenericTypeItem();
            model.Description = typeName;
            model.Description_En = typeNameEn;
            model.UniqueString = typeLink;
            model.MetaDescription = metaDes;
            model.MetaDescription_En = metaDesEn;
            model.MetaKeyword = metaKeyword;
            model.MetaKeyword_En = metaKeywordEn;

            if (int.TryParse(typeID, out typeIDInt))
            {
                model.TypeID = typeIDInt;
            }
            else
            {
                model.TypeID = null;
            }
            if (int.TryParse(parentID, out parentIDInt))
            {
                model.SupTypeID = parentIDInt;
            }
            else
            {
                model.SupTypeID = null;
            }

            model.CanEdit = Convert.ToBoolean(CanEdit);
            model.IsActive = true;
            model.LastUpdatedAt = DateTime.Now;
            model.CreatedAt = DateTime.Now;
            model.CreatedBy = CurrentUsername;
            model.LastUpdatedBy = CurrentUsername;

            if (string.IsNullOrWhiteSpace(model.Description) || string.IsNullOrWhiteSpace(model.Description_En))
            {
                return Redirect("/loi/thao-tac-khong-hop-le");
            }

            if (model.TypeID == null)
            {
                // Add new
                if (string.IsNullOrWhiteSpace(model.UniqueString) || _administrativeService.CheckUniqueStringGT(model.SupTypeID, model.TypeID, model.UniqueString))
                {
                    return Redirect("/loi/thao-tac-khong-hop-le");
                }

                result = _administrativeService.AddNewGenericType(model);
            }
            else if (model.TypeID != null)
            {
                // Edit
                result = _administrativeService.UpdateGenericType(model);
            }
            
            if (result)
            {
                AppContentTypes.SignalExpired(true);
                return View(AppContentTypes.All.FindAll(e => !e.CanEdit.HasValue || e.CanEdit.Value));
            }
            else
            {
                return Redirect("/loi/thao-tac-khong-hop-le");
            }
        }

        [HttpPost]
        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public bool CheckUniqueStringGT(string supTypeID, string typeID, string uniqueString)
        {
            int parentIDInt;
            int typeIDInt;
            int? parentID;
            int? tID;
            if (!int.TryParse(supTypeID, out parentIDInt))
            {
                parentID = null;
            }
            else { parentID = parentIDInt; }
            if (!int.TryParse(typeID, out typeIDInt))
            {
                tID = null;
            }
            else { tID = typeIDInt; }
            return _administrativeService.CheckUniqueStringGT(parentID, tID, uniqueString);
        }

        [HttpPost]
        [VTLAuthorize(RoleID = VTLPermission.Admin)]
        public ActionResult xoa_loai_san_pham(string typeID)
        {
            bool result = false;
            int typeIDInt;
            var model = new GenericTypeItem();
            model.TypeID = Convert.ToInt32(typeID);
            model.IsActive = false;
            model.LastUpdatedAt = DateTime.Now;
            model.LastUpdatedBy = CurrentUsername;
            if (int.TryParse(typeID, out typeIDInt))
            {
                model.TypeID = typeIDInt;
            }
            else
            {
                model.TypeID = null;
            }
            if (model.TypeID == null)
            {
                return Redirect("/vuarao/quan-ly-loai-san-pham");
            }
            else if (model.TypeID != null)
            {
                result = _administrativeService.DeleteGenericType(model);
            }

            if (result)
            {
                AppContentTypes.SignalExpired(true);
                return Redirect("/vuarao/quan-ly-loai-san-pham");
            }
            else
            {
                return Redirect("/loi/thao-tac-khong-hop-le");
            }
        }

        public ActionResult quan_ly_don_hang()
        {
            PagingInfoModel pagingInfo = this.GetPagingParameters();

            string productType = Request.Form["filter_productType"];

            if (string.IsNullOrWhiteSpace(productType))
            {
                productType = null;
            }

            string productSupType = Request.Form["filter_productSupType"];

            if (string.IsNullOrWhiteSpace(productSupType))
            {
                productSupType = null;
            }

            List<OrderModel> orderList = _administrativeService.GetOrderListByPagingInfo(productSupType, productType, ref pagingInfo);

            ViewBag.PagingInfo = pagingInfo;

            ViewBag.currentFilterProduct = productType;
            ViewBag.currentFilterSupProduct = productSupType;

            return View(orderList);
        }

        private int GetTypeIdValue(string typeID)
        {
            GenericTypes typeIdValue = GenericTypes.Unknown; ;

            if (typeID == GenericTypes.BaoDeoCacLoai.ToString())
            {
                typeIdValue = GenericTypes.BaoDeoCacLoai;
            }
            else if (typeID == GenericTypes.DayDeoThe.ToString())
            {
                typeIdValue = GenericTypes.DayDeoThe;
            }
            else if (typeID == GenericTypes.DayLua.ToString())
            {
                typeIdValue = GenericTypes.DayLua;
            }
            else if (typeID == GenericTypes.DayPoly.ToString())
            {
                typeIdValue = GenericTypes.DayPoly;
            }
            else if (typeID == GenericTypes.DichVuCacLoai.ToString())
            {
                typeIdValue = GenericTypes.DichVuCacLoai;
            }
            else if (typeID == GenericTypes.HuongDanThanhToan.ToString())
            {
                typeIdValue = GenericTypes.HuongDanThanhToan;
            }
            else if (typeID == GenericTypes.KhachHang.ToString())
            {
                typeIdValue = GenericTypes.KhachHang;
            }
            else if (typeID == GenericTypes.LienHe.ToString())
            {
                typeIdValue = GenericTypes.LienHe;
            }
            else if (typeID == GenericTypes.SiteMap.ToString())
            {
                typeIdValue = GenericTypes.SiteMap;
            }
            else if (typeID == GenericTypes.TheBaoHanh.ToString())
            {
                typeIdValue = GenericTypes.TheBaoHanh;
            }
            else if (typeID == GenericTypes.TheLoaiKhac.ToString())
            {
                typeIdValue = GenericTypes.TheLoaiKhac;
            }
            else if (typeID == GenericTypes.TheNhanVien.ToString())
            {
                typeIdValue = GenericTypes.TheNhanVien;
            }
            else if (typeID == GenericTypes.TheNhua.ToString())
            {
                typeIdValue = GenericTypes.TheNhua;
            }
            else if (typeID == GenericTypes.TheSinhVien.ToString())
            {
                typeIdValue = GenericTypes.TheSinhVien;
            }
            else if (typeID == GenericTypes.TheVIPMember.ToString())
            {
                typeIdValue = GenericTypes.TheVIPMember;
            }
            else if (typeID == GenericTypes.TinTuc.ToString())
            {
                typeIdValue = GenericTypes.TinTuc;
            }

            return typeIdValue != GenericTypes.Unknown ? Convert.ToInt32(typeIdValue) : -1;
        }

        private Dictionary<string, string> SelectDistinctAuthors(List<ProductModel> productList)
        {
            Dictionary<string, string> distinctAuthors = null;

            if (productList != null && productList.Count > 0)
            {
                distinctAuthors = new Dictionary<string, string>();

                foreach (var product in productList)
                {
                    var author = product.CreatedBy.ToString();

                    if (!string.IsNullOrWhiteSpace(author) && !distinctAuthors.ContainsKey(author))
                    {
                        distinctAuthors.Add(author, author);
                    }
                }
            }

            return distinctAuthors;
        }

        private Dictionary<string, string> SelectDistinctAuthors(List<NewsModel> newsList)
        {
            Dictionary<string, string> distinctAuthors = null;

            if (newsList != null && newsList.Count > 0)
            {
                distinctAuthors = new Dictionary<string, string>();

                foreach (var news in newsList)
                {
                    var author = news.CreatedBy.ToString();

                    if (!string.IsNullOrWhiteSpace(author) && !distinctAuthors.ContainsKey(author))
                    {
                        distinctAuthors.Add(author, author);
                    }
                }
            }

            return distinctAuthors;
        }

    }
}
